package Simpleblog::Controller::Logon;

use strict;
use warnings;
use base 'Catalyst::Controller';

=head1 NAME

Simpleblog::Controller::Logon - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 login

Login our blog system.

=cut

sub login : Local {
    my ($self, $c) = @_;

    # Get the username and password from form
    my $username = $c->request->params->{username} || "";
    my $password = $c->request->params->{password} || "";

    # If the username and password values were found in form
    if ($username && $password) {
        # Attempt to log the user in
        if ($c->login($username, $password)) {
            # If successful, then let them use the application
            $c->res->redirect($c->req->param('goto') || '/myblog');
            return;
        } else {
            # Set an error message
            $c->stash->{error_msg} = "Bad username or password.";
        }
    }

    # If either of above don't work out, send to the login page
    $c->stash->{template} = 'logon/login.tt2';

}

=head2 logout

log out our system

=cut

sub logout : Local {

    my ($self, $c) = @_;

    # Clear the user's state
    $c->logout;

    # Send the user to the starting point
    $c->response->redirect($c->uri_for('/'));

}


=head1 AUTHOR

chylli,,,

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
